/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.sciPersonnelInfo.service;

import com.fzhucc.modules.sciPersonnelInfo.domain.SciPersonnelInfoResult;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.sciPersonnelInfo.repository.SciPersonnelInfoResultRepository;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.SciPersonnelInfoResultDto;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.SciPersonnelInfoResultQueryCriteria;
import com.fzhucc.modules.sciPersonnelInfo.service.mapstruct.SciPersonnelInfoResultMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2023-11-30
**/
@Service
@RequiredArgsConstructor
public class SciPersonnelInfoResultService {

    private final SciPersonnelInfoResultRepository sciPersonnelInfoResultRepository;
    private final SciPersonnelInfoResultMapper sciPersonnelInfoResultMapper;

    public Map<String,Object> queryAll(SciPersonnelInfoResultQueryCriteria criteria, Pageable pageable){
        Page<SciPersonnelInfoResult> page = sciPersonnelInfoResultRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(sciPersonnelInfoResultMapper::toDto));
    }

    public List<SciPersonnelInfoResultDto> queryAll(SciPersonnelInfoResultQueryCriteria criteria){
        return sciPersonnelInfoResultMapper.toDto(sciPersonnelInfoResultRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public SciPersonnelInfoResultDto findById(Integer id) {
        SciPersonnelInfoResult sciPersonnelInfoResult = sciPersonnelInfoResultRepository.findById(id).orElseGet(SciPersonnelInfoResult::new);
        ValidationUtil.isNull(sciPersonnelInfoResult.getId(),"SciPersonnelInfoResult","id",id);
        return sciPersonnelInfoResultMapper.toDto(sciPersonnelInfoResult);
    }

    @Transactional(rollbackFor = Exception.class)
    public SciPersonnelInfoResultDto create(SciPersonnelInfoResult resources) {
        return sciPersonnelInfoResultMapper.toDto(sciPersonnelInfoResultRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(SciPersonnelInfoResult resources) {
        SciPersonnelInfoResult sciPersonnelInfoResult = sciPersonnelInfoResultRepository.findById(resources.getId()).orElseGet(SciPersonnelInfoResult::new);
        ValidationUtil.isNull( sciPersonnelInfoResult.getId(),"SciPersonnelInfoResult","id",resources.getId());
        sciPersonnelInfoResult.copy(resources);
        sciPersonnelInfoResultRepository.save(sciPersonnelInfoResult);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            sciPersonnelInfoResultRepository.deleteById(id);
        }
    }

    public void download(List<SciPersonnelInfoResultDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SciPersonnelInfoResultDto sciPersonnelInfoResult : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("tb_sci_personnel_info 表     id", sciPersonnelInfoResult.getSpId());
            map.put("奖励及成果名称", sciPersonnelInfoResult.getHonor());
            map.put("授奖单位", sciPersonnelInfoResult.getAwardingunit());
            map.put("时间", sciPersonnelInfoResult.getWhattime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}